<script>
import Space from '@c/Space'
import LzPopconfirm from '@c/LzPopconfirm'
import { deleteBatch, forceDeleteBatch, restoreBatch } from '@/api/common'
import { getMessage } from '@/libs/utils'

export default {
  name: 'OptionForm',
  components: { Space,LzPopconfirm },
  data() {
    return {
    }
  },
  props: {
    anySelected: true,
    detailMode: false,
    createMode: false,
    editMode: false,
    pass_put: true,
    pass_restore: true,
    pass_delete: true,
    pass_force_delete: true,
    pass_restore: true,
    pass_truncate: true,
    redirectList: {
      type: Boolean,
      default: true,
    },
    redirectDetail: {
      type: Boolean,
      default: true,
    },
    selectedCount: 0,
    selectedRowKeys: {
      type: Array,
      default: [],
    },
    metaid: 0,
  },
  methods: {
    async deleteBatch() {
      if (!this.selectedCount) {
        return
      }
      await deleteBatch(this.metaid, this.selectedRowKeys)
      this.$message.success(getMessage('destroyed'))
      if (this.editMode) {
        const p = this.$route.path.split('/')
        this.$router.push('/' + p[1])
      } else {
        this.onRefresh()
      }
    },
    async forceDeleteBatch() {
      if (!this.selectedCount) {
        return
      }
      await forceDeleteBatch(this.metaid, this.selectedRowKeys)
      this.$message.success(getMessage('destroyed'))
      if (this.detailMode || this.editMode) {
        const p = this.$route.path.split('/')
        this.$router.push('/' + p[1])
      } else {
        this.onRefresh()
      }
    },
    async restoreBatch() {
      if (!this.selectedCount) {
        return
      }
      await restoreBatch(this.metaid, this.selectedRowKeys)
      this.$message.success(getMessage('success'))
      this.onRefresh()
    },
    async truncate() {
      await forceDeleteBatch(this.metaid, 'all')
      this.$message.success(getMessage('success'))
      this.onRefresh()
    },
    onRefresh() {
      this.$router.replace({
        path: this.$route.fullPath,
        query: {
          _refresh: Date.now(),
        },
      })
    },
  },
  render(h) {
    var listRoute = '/'+this.$route.path.split('/')[1];
    var detailRoute = '/'+this.$route.path.split('/')[1]+'/'+this.$route.path.split('/')[2];
    var editRoute = '/'+this.$route.path.split('/')[1]+'/'+this.$route.path.split('/')[2]+'/edit';
    var redirectListButton = null
    if (this.detailMode || this.editMode || this.createMode) {
      if (this.redirectList) {
        var redirectListButton = h(
          'router-link',
          {
            attrs: {
              to: listRoute,
            },
          },
          [h('a-button', '列表')]
        );
      }
    }
    var redirectEditButton = null
    if (this.detailMode && this.pass_put && !this.pass_force_delete) {
      var redirectEditButton = h(
        'router-link',
        {
          attrs: {
            to: editRoute,
          },
        },
        [h('a-button', '编辑')]
      );
    }
    var redirectDetailButton = null
    if (this.editMode) {
      if (this.redirectDetail) {
        var redirectDetailButton = h(
          'router-link',
          {
            attrs: {
              to: detailRoute,
            },
          },
          [h('a-button', '详情')]
        );
      }
    }
    var deleteButton = null
    if (this.pass_delete) {
      var deleteButton = <lz-popconfirm
        title="确认操作？"
        confirm={this.deleteBatch}
        disabled={this.anySelected}
      >
        <a-button disabled={this.anySelected} style="color:#f5222d">删除</a-button>
      </lz-popconfirm>
    }
    var forceDeleteButton = null
    if (this.pass_force_delete) {
      var forceDeleteButton = <lz-popconfirm
        title="确认操作？"
        confirm={this.forceDeleteBatch}
        disabled={this.anySelected}
      >
        <a-button disabled={this.anySelected} style="color:#f5222d">彻底删除</a-button>
      </lz-popconfirm>
    }
    var truncateButton = null
    if (this.pass_truncate) {
      var truncateButton = <lz-popconfirm
        title="确认操作？"
        confirm={this.truncate}
      >
        <a-button style="color:#f5222d">清空</a-button>
      </lz-popconfirm>
    }
    var restoreButton = null
    if (this.pass_restore) {
      var restoreButton = <lz-popconfirm
        title="确认操作？"
        confirm={this.restoreBatch}
        disabled={this.anySelected}
      >
        <a-button disabled={this.anySelected} style="color:#52c41a">恢复</a-button>
      </lz-popconfirm>
    }
    return (
      <space>
        {redirectListButton}
        {redirectEditButton}
        {redirectDetailButton}
        {deleteButton}
        {forceDeleteButton}
        {truncateButton}
        {restoreButton}
      </space>
    )
  },
}
</script>